package ro.imc.andrei.visual;

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ro.imc.andrei.beans.Course;
import ro.imc.andrei.ums.UMSDB;

public class AddCourseFrame extends JFrame {

	// Labels
	private JLabel courseNameLabel = new JLabel("Course Name");
	private JLabel mandatoryLabel = new JLabel("Mandatory");
	private JLabel IDLabel = new JLabel("Course ID");
	private JLabel semesterLabel = new JLabel("Semester");
	private JLabel yearOfStudyLabel = new JLabel("Year Of Study");

	// Text Fields
	private JTextField courseNameTextField = new JTextField();
	private JTextField IDTextField = new JTextField();

	// Combo Boxes
	private JComboBox<String> mandatoryComboBox = new JComboBox<String>();
	private JComboBox<String> semesterComboBox = new JComboBox<String>();
	private JComboBox<String> yearOfStudyComboBox = new JComboBox<String>();

	// Panel
	private JPanel mainPanel = new JPanel();

	// Save Button
	private JButton saveButton = new JButton("Save");
	private JButton cancelButton = new JButton("Cancel");
	
	private JFrame parent; 

	private static int WITDH = 400;
	private static int HEIGTH = 200;

	public AddCourseFrame(JFrame parent) {
		this.parent = parent;
		initFrame();
	}

	private void initFrame() {

		// Init Mandatory Combo Box
		mandatoryComboBox.removeAllItems();
		mandatoryComboBox.addItem("Yes");
		mandatoryComboBox.addItem("No");

		// Init Semester Combo Box
		semesterComboBox.removeAllItems();
		semesterComboBox.addItem("1");
		semesterComboBox.addItem("2");
		
		//Init Year of Study Combo Box
		yearOfStudyComboBox.removeAllItems();
		yearOfStudyComboBox.addItem("1");
		yearOfStudyComboBox.addItem("2");
		yearOfStudyComboBox.addItem("3");
		yearOfStudyComboBox.addItem("4");
		yearOfStudyComboBox.addItem("5");
		yearOfStudyComboBox.addItem("6");

		// Init Frame Details
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setSize(WITDH, HEIGTH);
		this.setTitle("Add Course");

		// Add Panel to Frame and set Panel layout
		this.add(mainPanel);
		mainPanel.setLayout(new GridLayout(0, 2));

		// Add labels, text fields, and button
		mainPanel.add(courseNameLabel);
		mainPanel.add(courseNameTextField);

		mainPanel.add(IDLabel);
		mainPanel.add(IDTextField);
		
		mainPanel.add(mandatoryLabel);
		mainPanel.add(mandatoryComboBox);

		mainPanel.add(semesterLabel);
		mainPanel.add(semesterComboBox);
		
		mainPanel.add(yearOfStudyLabel);
		mainPanel.add(yearOfStudyComboBox);

		mainPanel.add(saveButton);
		mainPanel.add(cancelButton);

		saveButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				saveButtonActionPerformed(evt);
			}
		});
		
		cancelButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				cancelButtonActionPerformed(evt);
			}
		});
	}
	
	private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
		parent.setVisible(true);
		this.dispose();
	}

	private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {
		Course course = new Course();
		
		//Set course values
		if(mandatoryComboBox.getSelectedItem().toString().equalsIgnoreCase("Yes"))
			course.setMandatory(true);
		else
			course.setMandatory(false);
		
		course.setYearOfStudy(Integer.parseInt(yearOfStudyComboBox.getSelectedItem().toString()));
		
		course.setSemester(Integer.parseInt(semesterComboBox.getSelectedItem().toString()));
		
		course.setCourseName(courseNameTextField.getText());
		course.setCourseId(Integer.parseInt(IDTextField.getText()));
		
		String[] fields = course.getFields();
		String[] values = course.getValues();
		
		//Get UMSDB Instance
		UMSDB db = UMSDB.getInstance();
		
		db.insertMethod("course", fields, values);		
		
	}
}
